//
//  CommentsVC.m
//  YaoHeAPP
//
//  Created by mac on 15/12/25.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "Define.h"
#import "CommentsVC.h"
#import "StarView.h"
#import "Masonry.h"
#import "Controller.h"
#import "CommentType.h"
#import "ProvinceAndCity.h"
#import "InputHelper/InputHelper.h"
#import "ShowMessageView.h"

#define grayTextColor [UIColor colorWithRed:173.f/255.f green:173.f/255.f blue:173.f/255.f alpha:1]
#define MsgTextColor [UIColor colorWithRed:248.f/255.f green:122.f/255.f blue:79.f/255.f alpha:1]
@interface CommentsVC ()
{
    NSInteger width;
    NSInteger height;

    NSString*score1,*score2,*score3;
    
    
    UIImageView*Img1,*Img2,*Img3;
    UILabel*Msg1,*Msg2,*Msg3;
    
    UITextView*textView;
    
    
    UICollectionView*_collectionView;
    

    BOOL juan_cai;//卷材
    BOOL ban__cai;//板材
    BOOL guan_cai;//管材
    BOOL xing_cai;//型材
    
}
@end

@implementation CommentsVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"CommentsVC"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"CommentsVC"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    juan_cai =[_detail.categoryName isEqualToString:@"卷材"];
    ban__cai =[_detail.categoryName isEqualToString:@"板材"];
    guan_cai =[_detail.categoryName isEqualToString:@"管材"];
    xing_cai =[_detail.categoryName isEqualToString:@"型材"];

    //放置评论类型的对象
    _dataArray=[[NSMutableArray alloc]init];
    _selectCtypeTag=[[NSMutableArray alloc]init];

    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"评论";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(width/2-70);
        make.width.offset(150);
        make.height.offset(30);
    }];
    //
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];
    
    
        UIView*backView=[[UIView alloc]init];
     backView.frame=CGRectMake(0, 70, width, 92);
       [self.view addSubview:backView];
    //第一行
    UILabel*label1=[[UILabel alloc]init];
    label1.frame=CGRectMake(5, 8, backView.frame.size.width, 20);
    label1.font=font14;
    label1.text=[NSString stringWithFormat:@"%@ %@/%@  %@", _detail.varietyName, _detail.materialName,_detail.surfaceName,_detail.specCode];
    [backView addSubview:label1];
    
    //第二行

    //数量
    NSString*quantity=@"";
    if (_detail.quantity.length==0){
        quantity=@"无";
    }
    if (_detail.quantity.length>0) {
        if (xing_cai==YES||juan_cai==YES){
            quantity=[NSString stringWithFormat:@"%@个",_detail.quantity];
        }
        if (ban__cai==YES){
            quantity=[NSString stringWithFormat:@"%@张",_detail.quantity];
        }
        if (guan_cai==YES) {
            quantity=[NSString stringWithFormat:@"%@支",_detail.quantity];
        }
    }
    
    UIImageView*imageL=[[UIImageView alloc]init];
    imageL.image=[UIImage imageNamed:@"ic_qiugou_location"];
    imageL.frame=CGRectMake(5, 36, 20, 20);
    [backView addSubview:imageL];
    UILabel*label2=[[UILabel alloc]init];
    //label2.frame=CGRectMake(5, 36, backView.frame.size.width, 20);
    label2.font=font14;
    label2.textColor=grayTextColor;
    label2.text=[NSString stringWithFormat:@"%@%@  %@",[ProvinceAndCity inputShengID: _detail.supplyProvinceID ],[ProvinceAndCity inputCityID:_detail.supplyCityID] ,quantity];
    
    [backView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(25);  make.top.offset(36);
        make.height.offset(20);
    }];
    
    //第三行
    UIImageView*imageTm=[[UIImageView alloc]init];
    imageTm.image=[UIImage imageNamed:@"ic_qiugou_time"];
    imageTm.frame=CGRectMake(5, 64, 20, 20);
    [backView addSubview:imageTm];

    UILabel*creatTime=[[UILabel alloc]init];
    NSMutableString *a = [[NSMutableString  alloc]initWithString:_detail.orderTime];
    NSString*str= [a substringWithRange:NSMakeRange(6, 13)];
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:[str doubleValue]/1000];
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *showtimeNew = [formatter1 stringFromDate:d];
    creatTime.text=showtimeNew;
    creatTime.font=font14;
    creatTime.textColor=grayTextColor;
    [backView addSubview:creatTime];
    [creatTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);  make.left.offset(25);
        make.height.offset(20);
    }];
    

    UIImageView*lineImg=[[UIImageView alloc]init];
    lineImg.backgroundColor=grayTextColor;
    [self.view addSubview:lineImg];
    [lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0),make.top.equalTo(backView.mas_bottom).offset(1);
        make.right.offset(0); make.height.offset(1);
    }];
    
    
    UILabel*titleL=[[UILabel alloc]init];
    titleL.text=@"对供应商进行评价";
    titleL.font=font16;
    [self.view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);   make.right.offset(-10);
        make.height.offset(20);  make.top.equalTo(lineImg.mas_bottom).offset(5);
       }];
    

    NSArray*txtAr=@[@"货品与描述相符:",@"卖家的服务态度:",@"物流的服务质量:"];
    //
    for (NSInteger i=0; i<3; i++) {
        UILabel*myLab=[[UILabel alloc]init];
        myLab.frame=CGRectMake(8, 200+i*32, 105, 18);
        myLab.text=txtAr[i];
        myLab.font=font14;
        myLab.textColor=grayTextColor;
        [self.view addSubview:myLab];
    }
    
    UIImage*img=[UIImage imageNamed:@"redKuang"];
    Img1=[UIImageView new];
    Img1.frame=CGRectMake(width-60, 195, 50, 26);
    Img1.image=img;
    Img1.hidden=YES;
    [self.view addSubview:Img1];
    Msg1=[UILabel new];
    Msg1.frame=CGRectMake(3, 0, 47, 26);
    Msg1.textColor=MsgTextColor;
    Msg1.font=font13;
    Msg1.textAlignment=NSTextAlignmentCenter;
    [Img1 addSubview:Msg1];
    [StarView showStartView:self.view WithFrame:CGRectMake(110, 195, width-110-60, 26) EmptyImage:@"b27_icon_star_gray" StarImage:@"b27_icon_star_yellow" andSelectBlock:^(NSString *str1) {
         score1=str1;
         NSInteger number=[str1 integerValue];
        if (number>0) {
            Img1.hidden=NO;
        }
        switch (number) {
            case 1:
            {
                Img1.hidden=NO;
                Msg1.text=@"很差";
            }
                break;
            case 2:
                Msg1.text=@"不喜欢";
                break;
            case 3:
                Msg1.text=@"一般";
                break;
            case 4:
                Msg1.text=@"还不错";
                break;
            case 5:
                Msg1.text=@"很给力";
                break;
            default:
                break;
        }
        
    }];
    
    
    Img2=[UIImageView new];
    Img2.frame=CGRectMake(width-60, 228, 50, 26);
    Img2.image=img;
    Img2.hidden=YES;
    [self.view addSubview:Img2];
    Msg2=[UILabel new];
    Msg2.frame=CGRectMake(3, 0, 47, 26);
    Msg2.textColor=MsgTextColor;
    Msg2.font=font13;
    Msg2.textAlignment=NSTextAlignmentCenter;
    [Img2 addSubview:Msg2];
    [StarView showStartView:self.view WithFrame:CGRectMake(110, 228, width-110-60, 26) EmptyImage:@"b27_icon_star_gray" StarImage:@"b27_icon_star_yellow" andSelectBlock:^(NSString *str2) {
        score2=str2;
        NSInteger number=[str2 integerValue];
        if (number>0) {
            Img2.hidden=NO;
        }
        switch (number) {
            case 1:
            {
                Msg2.text=@"很差";
            }
                break;
            case 2:
                Msg2.text=@"不喜欢";
                break;
            case 3:
                Msg2.text=@"一般";
                break;
            case 4:
                Msg2.text=@"还不错";
                break;
            case 5:
                Msg2.text=@"很给力";
                break;
            default:
                break;
        }

    }];

    
    Img3=[UIImageView new];
    Img3.frame=CGRectMake(width-60, 258, 50, 26);
    Img3.image=img;
    Img3.hidden=YES;
    [self.view addSubview:Img3];
    Msg3=[UILabel new];
    Msg3.frame=CGRectMake(3, 0, 47, 26);
    Msg3.textColor=MsgTextColor;
    Msg3.font=font13;
    Msg3.textAlignment=NSTextAlignmentCenter;
    [Img3 addSubview:Msg3];
    [StarView showStartView:self.view WithFrame:CGRectMake(110, 258, width-110-60, 26) EmptyImage:@"b27_icon_star_gray" StarImage:@"b27_icon_star_yellow" andSelectBlock:^(NSString *str3) {
        score3=str3;
        NSInteger number=[str3 integerValue];
        if (number>0) {
            Img3.hidden=NO;
        }
        switch (number) {
            case 1:
            {
                Img3.hidden=NO;
                Msg3.text=@"很差";
            }
                break;
            case 2:
                Msg3.text=@"不喜欢";
                break;
            case 3:
                Msg3.text=@"一般";
                break;
            case 4:
                Msg3.text=@"还不错";
                break;
            case 5:
                Msg3.text=@"很给力";
                break;
            default:
                break;
        }

    }];
 
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    //调取
    [Controller requestWithURL:@"AppEvaluationInfo/PostQueryEvaluationTitle?from=app" jsondic:nil finishBlock:^(ClassData *classData) {
        NSArray*dataAry=classData.data;
        for (NSDictionary*dic in dataAry) {
            CommentType*comType=[[CommentType alloc]initWithDic:dic];
            [_dataArray addObject:comType];
        }
        NSInteger jianju=30;
        CGRect txtFrame;
        for (NSInteger i=0; i<_dataArray.count; i++) {
            CommentType*comtypeTitle=_dataArray[i];
            UIButton*typeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            typeBtn.layer.masksToBounds=YES;
            typeBtn.layer.cornerRadius=16;
            [typeBtn setTitle:comtypeTitle.typeMsg forState:UIControlStateNormal];
            typeBtn.selected=NO;
            typeBtn.backgroundColor=[UIColor colorWithRed:229.f/255.f green:229.f/255.f blue:229.f/255.f alpha:1];
            typeBtn.titleLabel.font=[UIFont systemFontOfSize:13];
            typeBtn.tag=i;
            [typeBtn addTarget:self action:@selector(clickSelect:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:typeBtn];
            
            //自适应宽度
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16],};
            CGSize textSize = [comtypeTitle.typeMsg boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
            if(i==0){
                typeBtn.frame=CGRectMake(jianju-10, 300+5+(5+15)*(i/3), textSize.width+20, 32);
            }else{
                
                if (i%3==0) {
                    
                    typeBtn.frame=CGRectMake(jianju-10, 300+5+(5+15)*(i/3), textSize.width+20, 32);
                    
                }else{
                    
                    CommentType*comtypeTitle1=_dataArray[i-1];
                    
                    CGSize textSize1 = [comtypeTitle1.typeMsg boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
                    typeBtn.frame=CGRectMake(txtFrame.origin.x+jianju+textSize1.width, 300+5+(5+15)*(i/3), textSize.width+20, 32);
                    
                }
                
            }
            
            txtFrame=typeBtn.frame;

            
            
            
            
        }
//        top.offset(300+37*(_dataArray.count/2)+5)
              //输入框
        textView=[[UITextView alloc]init];
        textView.layer.borderColor=grayTextColor.CGColor;
        textView.layer.borderWidth=0.5;
        [self.view addSubview:textView];
        [textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(txtFrame.origin.y+32+20);
            make.left.offset(20);   make.right.offset(-20);
            make.height.offset(60);
        }];
        
        //立即提交
        UIButton*commentBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        commentBtn.layer.masksToBounds=YES;
        commentBtn.layer.cornerRadius=20;
        [commentBtn setBackgroundImage:[UIImage imageNamed:@"orangeButton_13"] forState:UIControlStateNormal];
        [commentBtn setBackgroundImage:[UIImage imageNamed:@"orangeButton_13_hl"] forState:UIControlStateHighlighted];
        [commentBtn addTarget:self action:@selector(commentBtn) forControlEvents:UIControlEventTouchUpInside];
        [commentBtn setTitle:@"立即提交" forState:UIControlStateNormal];
        [commentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:commentBtn];
        [commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(30);
            make.right.offset(-20);
            make.height.offset(40);
            make.top.equalTo(textView.mas_bottom).offset(30);
        }];
}];
}
//选择评论类型
-(void)clickSelect:(UIButton*)seletBtn
{
    if (seletBtn.selected==YES) {
        CommentType*typeID=_dataArray[seletBtn.tag];
        [_selectCtypeTag removeObject:typeID.value];
        seletBtn.selected=NO;
        
        seletBtn.backgroundColor=[UIColor colorWithRed:229.f/255.f green:229.f/255.f blue:229.f/255.f alpha:1];

        return;
    }
    if (seletBtn.selected==NO) {
        CommentType*typeID=_dataArray[seletBtn.tag];
        [_selectCtypeTag addObject:typeID.value];
        seletBtn.selected=YES;
        
         seletBtn.backgroundColor=[UIColor colorWithRed:251.f/255.f green:87.f/255.f blue:60.f/255.f alpha:1];
        
        return;
    }
}

//立即提交按钮
-(void)commentBtn
{

    NSString*typeValueID=[_selectCtypeTag componentsJoinedByString:@","];
    if (score1.length==0||score2.length==0||score3==0) {
        [ShowMessageView  showMessage:@"请选择评价分数"];
        return;
    }
    if (typeValueID.length==0) {
        [ShowMessageView  showMessage:@"请选择评价标签"];
        return;
    }
    
    
    NSDictionary*dic=@{@"USER_ID":_detail.sellerUserID,
                       @"COMPANY_ID":_detail.sellerCompanyID,
                       @"COMPANY_NAME":_detail.sellerCompanyName,
                       @"ORDER_ID":_detail.orderID,
                       @"PRODUCT_SCORE":score1,
                       @"SERVICE_SCORE":score2,
                       @"LOGISTICS_SCORE":score3,
                       @"EVALUATION_TITLE":typeValueID,
                       @"REMARK":textView.text};
    
    [Controller requestWithURL:@"AppEvaluationInfo/PostSaveRecord?from=app" jsondic:dic finishBlock:^(ClassData *deta) {
        if ([deta.number integerValue]==0) {
            
            [self.navigationController popViewControllerAnimated:YES];
            [ShowMessageView  showMessage:@"评论成功"];

        }
        else{
            ALERTSHOW(deta.message);
        }
    }];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)keyBoardFrameChange:(NSNotification *)notification
{
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
            NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
            NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
            CGRect keyboardBounds;
            [keyboardBoundsValue getValue:&keyboardBounds];
            NSInteger offset =self.view.frame.size.height-keyboardBounds.origin.y;
            CGRect listFrame = CGRectMake(0, -offset, self.view.frame.size.width,self.view.frame.size.height);
            [UIView beginAnimations:@"anim" context:NULL];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationBeginsFromCurrentState:YES];
            [UIView setAnimationDuration:0.2];
            //处理移动事件，将各视图设置最终要达到的状态
            self.view.frame=listFrame;
            [UIView commitAnimations];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
